<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
       <style>
           * {
               margin: 0;
               padding: 0;
           }
           #box {
               width: 1000px;
               margin: 0 auto;
               position: relative;
           }
           #pageHeader {
               width: 100%;
               height: 200px;
               background: slateblue;
           }
           #pageNav {
               width: 1000px;
               height: 50px;
               background: skyblue;
           }
           #content1,#content2,#content3 {
               width: 100%;
               height: 500px;
               background: steelblue;
           }
           #content2 {
               background: tomato;
           }
           #content3 {
            background: violet;
           }
       </style>
</head>
<body>
    <div id="box">
        <div id="pageHeader"></div>
        <div id="pageNav"></div>
        <div id="content1"></div>
        <div id="content2"></div>
        <div id="content3"></div>
    </div>
    <script>
        var nav = document.querySelector('#pageNav')
        var nt = nav.offsetTop
        window.onscroll = function() {
            var ct = document.documentElement.scrollTop
            if(ct>nt){
                nav.style.position = 'fixed'
                nav.style.top = '0px'
            } else {
                nav.style.position = 'relative'
            }
        }
    </script>
</body>
</html>